package com.puman.watchtrade.fragment.home.imageLoader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.imageLoader.adapter.PhotoAdapter;
import com.puman.watchtrade.fragment.home.imageLoader.bean.PhotoInfo;
import com.puman.watchtrade.fragment.home.imageLoader.bean.PhotoSerializable;
import com.puman.watchtrade.fragment.sell.util.UniversalImageLoadTool;
import com.puman.watchtrade.util.GlobalData;

/**    
 */
public class PhotoFragment extends Fragment {

	public interface OnPhotoSelectClickListener {
		public void onPhotoSelectClickListener(List<PhotoInfo> list);

	}

	private OnPhotoSelectClickListener onPhotoSelectClickListener;

	private GridView gridView;
	private PhotoAdapter photoAdapter;

	private List<PhotoInfo> list;

	private int hasSelect = 1;

	private int count;

	private TextView selectNo;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		if (onPhotoSelectClickListener == null) {
			onPhotoSelectClickListener = (OnPhotoSelectClickListener) activity;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater
				.inflate(R.layout.fragment_photoselect, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		gridView = (GridView) getView().findViewById(R.id.gridview);
		selectNo = (TextView) getView().findViewById(R.id.select_no);

		getView().findViewById(R.id.sure_select).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
//						ShareTemplateFragment.photoAdapter
//								.notifyDataSetChanged();
						SelectPhotoActivity.getSelectPhotoActivity().finish();
					}
				});

		Bundle args = getArguments();

		PhotoSerializable photoSerializable = (PhotoSerializable) args
				.getSerializable("list");
		list = new ArrayList<PhotoInfo>();
		list.addAll(photoSerializable.getList());
		hasSelect += count;

		photoAdapter = new PhotoAdapter(getActivity(), list, gridView);
		gridView.setAdapter(photoAdapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (list.get(position).isChoose() && hasSelect > 1) {
					list.get(position).setChoose(false);
					hasSelect--;
				} else if (hasSelect <= 10) {
					list.get(position).setChoose(true);
					hasSelect++;
				} else {
					Toast.makeText(getActivity(), "最多选择10张图片！",
							Toast.LENGTH_SHORT).show();
				}
				int selNo = 0;
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).isChoose()) {
						selNo++;
					}
				}
				GlobalData.photoList.clear();
				for (int j = 0; j < list.size(); j++) {
					if (list.get(j).isChoose()) {
						GlobalData.photoList.add(list.get(j));
					}
				}
				selectNo.setText(selNo + "");
				photoAdapter.refreshView(position);
				onPhotoSelectClickListener.onPhotoSelectClickListener(list);
			}
		});

		gridView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == 0) {
					UniversalImageLoadTool.resume();
				} else {
					UniversalImageLoadTool.pause();
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
	}
}
